/*
StringFunctions.js
Functions for Manipulating Strings
Last update: 05/07/2011

Mikhail Breslav and Chris Kwan
{breslav, ckwan}@cs.bu.edu
http://cs-people.bu.edu/ckwan/cs511/

CS 511 Spring 2011 Project
Client-side Parsing and Verification
*/

/*
 Return a new version of str with the first word removed.
*/
function chompOffFirstWord(str) 
{
    //code copied from getFirstWord(str)
    var newStr = str;
    
    //remove any leading spaces
    newStr = skipLeadingBlanks(newStr);
    
    //find the index of the first blank,
    //which marks the end of the first word
    var firstBlankIndex = 0;
    
    while( !isBlankChar(newStr[firstBlankIndex])
        && firstBlankIndex < newStr.length)
    {
        firstBlankIndex++;
    }
        
    //return str with the first word removed
    //it may have leading spaces
    //substring(first-index-you-want, first-index-you-don't-want)
    return newStr.substring(firstBlankIndex, newStr.length);
}


/*
 Return the first word in str without modifying str.
*/
function getFirstWord(str)
{
    var newStr = str;
    
    //remove any leading spaces
    newStr = skipLeadingBlanks(newStr);
    
    //find the index of the first blank,
    //which marks the end of the first word
    var firstBlankIndex = 0;
    
    while( !isBlankChar(newStr[firstBlankIndex])
        && firstBlankIndex < newStr.length)
    {
        firstBlankIndex++;
    }
    
    //return the first word
    //substring(first-index-you-want, first-index-you-don't-want)
    return newStr.substring(0, firstBlankIndex);
}


/* 
 Return a new version of str with the first character removed.
*/
function chompOffFirstChar(str)
{
    //substring(first-index-you-want, first-index-you-don't-want)
    return str.substring(1, str.length);
}


/*
 Return the next character in str without modifying str.
*/
function getFirstChar(str)
{
    return str[0];
}


/*
 Return new version of str with leading blank characters removed.
*/
function skipLeadingBlanks(str)
{
    //find index of first non-blank character
    var firstNonBlankIndex = 0;
    while(isBlankChar(str[firstNonBlankIndex]) 
        && firstNonBlankIndex < str.length)
    {
        firstNonBlankIndex++;
    }
    
    //substring(first-index-you-want, first-index-you-don't-want)
    return str.substring(firstNonBlankIndex, str.length);
}


/*
 Return true if a character is a blank (whitespace) character.
*/
function isBlankChar(val)
{
    if(val == " " || val == "\t" || val == "\n" || val == "\r")
    {
        return true;
    }
    else
    {
        return false;
    }
}

